/**
 * 
 */
package com.youxin.cpcenter.pt.service.process;

import org.springframework.beans.BeanUtils;

import com.youxin.busfoundation.bean.ent.RequestEntIdentifyInfo;
import com.youxin.busfoundation.bean.ent.RequestMemberInfo;
import com.youxin.busfoundation.bean.login.RequestLoginBean;
import com.youxin.busfoundation.bean.login.RequestLoginVerifyCode;
import com.youxin.busfoundation.common.es.bean.CompanyInfoBean;
import com.youxin.busfoundation.common.redis.RedisOperator;
import com.youxin.busfoundation.enums.EnterpriseIdentityType;
import com.youxin.cpcenter.bean.MemberQueryListBean;
import com.youxin.cpcenter.pt.bean.ResponseListMember;
import com.youxin.cpcenter.pt.bean.ResponseLogin;
import com.youxin.cpcenter.pt.bean.ResponseOutMessage;
import com.youxin.cpcenter.pt.bean.ResponseTryout;
import com.youxin.cpcenter.pt.service.BaseOperation;
import com.youxin.cpcenter.pt.service.login.LoginPt;
import com.youxin.cpcenter.pt.service.member.AddMember;
import com.youxin.cpcenter.pt.service.member.ListMember;
import com.youxin.cpcenter.pt.service.tryout.IdentifyPt;
import com.youxin.cpcenter.pt.service.tryout.TryoutPt;
import com.youxin.foundation.db.utils.KeyGenerationUtils;
import com.youxin.foundation.interfaces.ExecuteFunction2;
import com.youxin.foundation.utils.RandomUtils;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;

/**
 * @author yingde.cao
 * @date 2017年12月23日
 * @version 1.0
 */
public class Identify extends BaseOperation {

	public static void identify(CompanyInfoBean bean, ExecuteFunction2<ResponseOutMessage, CompanyInfoBean> execute) {
		// Observable<CompanyInfoBean> observable = Observable.from(bean);
		Observable<CompanyInfoBean> observable = Observable.just(bean);
		Subscriber<ResponseTryout> subscriber = new Subscriber<ResponseTryout>() {
			@Override
			public void onNext(ResponseTryout t) {
				if (t.success() == false) {
					logger.error("IdentifyPt异常:" + t.toString());
					return;
				}
				long id = 0L;
				try {
					id = Long.valueOf(t.getData());
				} catch (Exception e) {
					logger.error(e);
				}
				RequestEntIdentifyInfo identifyInfo = new RequestEntIdentifyInfo();
				identifyInfo.setId(id);
				identifyInfo.setCompanyUid(bean.getUid());
				identifyInfo.setFullName(bean.getFullName());
				identifyInfo.setMemberId(0L);
				identifyInfo.setIdentifyType(EnterpriseIdentityType.Normal.getValue());
				ResponseOutMessage identifyBean = IdentifyPt.postMethod(identifyInfo);
				if (identifyBean.success()) {
					execute.execute(identifyBean, bean);
				}
			}

			@Override
			public void onCompleted() {

			}

			@Override
			public void onError(Throwable e) {

			}
		};
		observable.subscribe(new Action1<CompanyInfoBean>() {
			@Override
			public void call(CompanyInfoBean t) {
				ResponseTryout outBean = TryoutPt.postMethod(t);
				subscriber.onNext(outBean);
			}
		});

		// observable.subscribe(new Action1<CompanyInfoBean>() {
		//
		// @Override
		// public void call(CompanyInfoBean s) {
		// // System.out.println("Hello " + s + "!");
		// }
		//
		// });
	}

	public static void identifyAndLogin(CompanyInfoBean bean) {
		ResponseTryout outBean = TryoutPt.postMethod(bean);
		if (outBean.success() == false) {
			logger.error("IdentifyPt异常:" + outBean.toString());
			return;
		}
		long id = 0L;
		try {
			id = Long.valueOf(outBean.getData());
		} catch (Exception e) {
			logger.error(e);
		}
		
		/*
		 * 产品认证
		 */		
		RequestEntIdentifyInfo identifyInfo = new RequestEntIdentifyInfo();
		identifyInfo.setId(id);
		identifyInfo.setCompanyUid(bean.getUid());
		identifyInfo.setFullName(bean.getFullName());
		identifyInfo.setMemberId(0L);
		identifyInfo.setIdentifyType(EnterpriseIdentityType.Vip.getValue());
		ResponseOutMessage identifyBean = IdentifyPt.postMethod(identifyInfo);
		if (!identifyBean.success()) {
			logger.error(outBean.toString());
			return;
		}
		
		RequestLoginBean loginBean=new RequestLoginBean();
		loginBean.setMobile(bean.getPhone());
		loginBean.setCode(TryoutPt.DEFAULT_CODE);			
		RequestLoginVerifyCode loginVerifyCode=new RequestLoginVerifyCode();
		BeanUtils.copyProperties(loginBean, loginVerifyCode);
		RedisOperator.saveLoginVerifyCode(loginVerifyCode);
		
		ResponseLogin outLogin=LoginPt.postMethod(loginBean,bean);
		if(!outLogin.success())
		{
			logger.error(outLogin.toString());
			return;
		}
		RequestMemberInfo requestMemberInfo=new RequestMemberInfo();
		requestMemberInfo.setAreaCode("+86");
		requestMemberInfo.setMobile("1"+RandomUtils.randomDigit(10));
		requestMemberInfo.setName(String.valueOf(KeyGenerationUtils.generateKey()));
		requestMemberInfo.setName(RandomUtils.randomString(20));
		requestMemberInfo.setPosition("测试职位"+RandomUtils.randomString(6));
		ResponseOutMessage responseOutMessage=AddMember.postMethod(requestMemberInfo, bean);
		if(!responseOutMessage.success())
		{
			logger.error(responseOutMessage.toString());
			return;
		}
		
		MemberQueryListBean listBean=new MemberQueryListBean();
		ResponseListMember listMember=ListMember.postMethod(listBean,bean);
		if(!listMember.success()){
			logger.error(listMember.toString());
			return;
		}
		
		ResponseOutMessage outMessage=ListMember.auth(bean, listMember);
		if(outMessage!=null&&!outBean.success())
		{
			logger.error(outBean.toString());
		}
		
		outMessage=ListMember.unAuth(bean, listMember);
		if(outMessage!=null&&!outBean.success())
		{
			logger.error(outBean.toString());
		}
		
		outMessage=ListMember.open(bean, listMember);
		if(outMessage!=null&&!outBean.success())
		{
			logger.error(outBean.toString());
		}
		
		outMessage=ListMember.close(bean, listMember);
		if(outMessage!=null&&!outBean.success())
		{
			logger.error(outBean.toString());
		}
		
//		
//		
//		Subscriber<RequestLoginBean> subscriber = new Subscriber<RequestLoginBean>() {
//			@Override
//			public void onNext(RequestLoginBean t) {
//				ResponseLogin outLogin=LoginPt.postMethod(t,bean);
//				System.out.println(t.toString());
//				if(!outLogin.success())
//				{
//					logger.error(outLogin.toString());
//					return;
//				}
//				MemberQueryListBean listBean=new MemberQueryListBean();
//				ResponseListMember listMember=ListMember.postMethod(listBean,bean);
//				if(!listMember.success()){
//					logger.error(listMember.toString());
//					return;
//				}
//				RequestMemberInfo requestMemberInfo=new RequestMemberInfo();
//				requestMemberInfo.setAreaCode("+86");
//				requestMemberInfo.setMobile("1"+RandomUtils.randomDigit(10));
//				requestMemberInfo.setName(String.valueOf(KeyGenerationUtils.generateKey()));
//				requestMemberInfo.setName(RandomUtils.randomString(20));
//				requestMemberInfo.setPosition("测试职位"+RandomUtils.randomString(6));
//				ResponseOutMessage responseOutMessage=AddMember.postMethod(requestMemberInfo, bean);
//				if(!responseOutMessage.success())
//				{
//					logger.error(responseOutMessage.toString());
//					return;
//				}
//			}
//
//			@Override
//			public void onCompleted() {
//				
//			}
//
//			@Override
//			public void onError(Throwable e) {
//				
//			}
//		};
		
//		identify(bean, (t, p) -> {
//			RequestLoginBean loginBean=new RequestLoginBean();
//			loginBean.setMobile(p.getPhone());
//			loginBean.setCode(TryoutPt.DEFAULT_CODE);			
//			RequestLoginVerifyCode loginVerifyCode=new RequestLoginVerifyCode();
//			BeanUtils.copyProperties(loginBean, loginVerifyCode);
//			RedisOperator.saveLoginVerifyCode(loginVerifyCode);
//			subscriber.onNext(loginBean);
//		});
	}

}
